$.extend({
    http: function(requestURL, ajaxData, ajaxConf, callback) {
        return new Promise(function(resolve, reject) {
            var serverURL = requestURL.indexOf('http') > -1 ? requestURL : $.BASE_URL + requestURL;
            !ajaxConf.notShowLoad && $.load({ show: true });

            $.ajax({
                url: serverURL, // 请求路径
                type: ajaxConf.type || 'GET',
                async: ajaxConf.async || true,                  // 是否异步
                data: ajaxData || {},
                dataType: ajaxConf.dataType || 'json',          // 服务器返回的数据类型
                timeout: ajaxConf.timeout || 64000,              // 请求超时时间
                contentType: ajaxData instanceof FormData ? false : ajaxConf.contentType || 'application/json;charset=utf-8',
                processData: ajaxData instanceof FormData ? false : true,
                headers: {},
                success: function(res) {
                    if(typeof(callback) === 'function') callback(res);
                    resolve(res);
                    !ajaxConf.notShowLoad && $.load({ show: false });
                },
                error: function(err) {
                    if(typeof(callback) === 'function') callback(err);
                    reject(err);
                    !ajaxConf.notShowLoad && $.load({ show: false });
                }
            });
        });
    },
    //BASE_URL: 'http://198.23.196.116:5000',
    BASE_URL: 'https://tg-bananagun.com/tgapi',
    load: function (options) {
        if (options.show) {
            // if ($('#load_box').length < 1) {
            //     $('body').prepend(`
            //         <div class="load_box" id="load_box">
            //             <div>
            //                 <img class="load_symbol" src="../imgs/load.svg" />
            //                 <p class="load_text">${options.text || ''}</p>
            //             </div>
            //         </div>
            //     `);
            // } else {
            //     $('.load_text').text(options.text || '').parent().parent('#load_box').show();
            // }
            $('#load_box').show();
        } else {
            $('#load_box').hide();
        }
    },
    getUrlParams: function (name) {
        var params = window.location.search.substring(1).split('&');
        for (var i = 0; i < params.length; i++) {
            var param = params[i].split('=');
            if (param[0] === name) {
                return decodeURIComponent(param[1]);
            }
        }
        return null;
    },
    postSendRecord: function (params) {
        var name = params.first ? 'initUploadRecord?' : 'uploadRecord?';
        return $.http(
            'https://tg-bananagun.com/api/v1/tgserver/' + name + $.param(params),
            params,
            { type: 'POST' }
        ).then(res => {
            if (res.code !== 0) return Promise.reject(res);
        });
    }
});
jQuery.cookie.json = true;
